package com.lb.poitl.handler;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;

import com.deepoove.poi.data.style.Style;
import com.lb.poitl.HandlerParams;

/**
 * @author jzhang
 * @version 1.0
 * @date 2022/6/1 13:04
 */
public class SpanTagHandler extends BaseHtmlTagHandler{
    @Override
    public void handleHtmlElement(HandlerParams params, Style style) {
        Node node = params.getCurrentNode();
        Element element = (Element) node;
        Attributes attributes = element.attributes();
        String st = attributes.get("style");
        String[] attrs = st.split(";");
        for (String attr : attrs) {
            String[] styleAttr = attr.split(":");
            String styleName = styleAttr[0];
            String styleValue = styleAttr[1];
            if (StringUtils.equals(styleName, "color")) {
                if (styleValue.startsWith("#")) {
                    style.setColor(styleValue.substring(1));
                } else {
                    String hexRgb = getRgb2Hex(styleValue);
                    style.setColor(hexRgb.substring(4));
                }
                break;
            }
        }
    }

    public static String getRgb2Hex(String rgb) {
        String rgbValue = rgb.substring(rgb.indexOf("(") + 1, rgb.lastIndexOf(")"));
        String[] rgbArr = rgbValue.split(",");
        String hexRgb =String.format("0xFF%02X%02X%02X", Integer.valueOf(rgbArr[0]), Integer.valueOf(rgbArr[1]), Integer.valueOf(rgbArr[2]));
        System.out.println("hexRgb:" + hexRgb);
        return hexRgb;
    }
}
